import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.List;
import java.util.Set;

public class test_20251031 {
    public void test01(){
        WebDriverManager.chromedriver().setup();
        //edgedriver
//        WebDriverManager.edgedriver().setup();

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");

        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);

        //2.输入百度网址:https://www.baidu.com
        driver.get("https://www.baidu.com");


        ////元素的定位////

//        //查找百度输入框
//        driver.findElement(By.cssSelector("#chat-textarea"));
//        //查找百度一下按钮
//        driver.findElement(By.cssSelector("#chat-submit-button"));


//        //查找百度输入框--xpath
//        driver.findElement(By.xpath("//*[@id=\"chat-textarea\"]"));
//        //查找百度一下按钮--xpath
//        driver.findElement(By.xpath("//*[@id=\"chat-submit-button\"]"));


        ///////////查找findements////////////
        List<WebElement> eles = driver.findElements(By.cssSelector("#hotsearch-content-wrapper > li"));
        List<WebElement> eles2 = driver.findElements(By.xpath("//*[@id=\"hotsearch-content-wrapper\"]/li/a/span[2]"));
        for (WebElement e: eles2) {
            System.out.println(e.getText());
        }


        driver.quit();
    }

    public void test02() throws InterruptedException {
        WebDriverManager.chromedriver().setup();

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");

        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);

        //2.输入百度网址:https://www.baidu.com
        driver.get("https://www.baidu.com");

        /////////////对元素操作///////////
        //输入框--输入文本信息
//        WebElement element = driver.findElement(By.cssSelector("#chat-textarea"));
//        element.sendKeys("123456456");

//        //合并成一句
//        driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("123456");
//
//        //按钮--点击
//        driver.findElement(By.cssSelector("#chat-submit-button")).click();
//
//        Thread.sleep(3000);

//        //查找百度输入框
//        WebElement element = driver.findElement(By.cssSelector("#chat-textarea"));
//        //输入迪丽热巴
//        element.sendKeys("迪丽热巴");
//        Thread.sleep(1000);
//        //清空输入框内容
//        element.clear();
//        //输入邓紫棋
//        element.sendKeys("邓紫棋");
//        Thread.sleep(1000);

//        String newsText = driver.findElement(By.cssSelector("#hotsearch-content-wrapper > li:nth-child(2) > a > span.title-content-title")).getText();
//        System.out.println("newsText"+newsText);
//
//        //获取百度一下按钮上的文本“百度一下”
//        String baiduText = driver.findElement(By.xpath("//*[@id=\"chat-submit-button\"]")).getText();
//        System.out.println("baidu一下按钮:"+baiduText);
//
//        String text = driver.findElement(By.xpath("//*[@id=\"s-hotsearch-wrapper\"]/div/a[1]/div")).getAttribute("aria-label");
//        System.out.println(text);

        //获取跳转前标题和URL
        String titleBefore = driver.getTitle();
        String urlBefore = driver.getCurrentUrl();

        System.out.println("titleBefore:" + titleBefore);
        System.out.println("urlBefore:" + urlBefore);

        //百度首页点击“图片”超链接跳转到图片页面
        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(6)")).click();
        Thread.sleep(3000);

        //百度图片页面的句柄和百度图片的句柄
        String curHandle = driver.getWindowHandle();
        Set<String> allHands = driver.getWindowHandles();
        //driver切换到百度图片标签页
        for (String handle : allHands){
            if (!handle.equals(curHandle)){
                //切换
                driver.switchTo().window(handle);
            }
        }


        //获取跳转后的标题和URL
        String titleAfter = driver.getTitle();
        String urlAfter = driver.getCurrentUrl();
        System.out.println("titleAfter:"+titleAfter);
        System.out.println("urlAfter:"+urlAfter);

        driver.quit();
    }

    /**
     * 屏幕截图
     * @throws InterruptedException
     * @throws IOException
     * image/
     *      2025-10-31/
     *                  funcName_20251031_060840500.png
     *      2025-11-01/
     *                  funcName_20251101_060840500.png
     */
    public void getScreenShot(String str,WebDriver driver) throws IOException {
        SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sim2 = new SimpleDateFormat("HHmmssSS");

        String dirTime = sim1.format(System.currentTimeMillis());
        String fileTime = sim2.format(System.currentTimeMillis());

        //  image/2025-10-31/funcName_060840500.png
        String fileName = "./src/test/images/" + dirTime + "/" + str + "-" + fileTime + ".png";

        File srcfile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        //file -----> .png file
        FileUtils.copyFile(srcfile,new File(fileName));
    }

    public void test03() throws InterruptedException, IOException {
        WebDriverManager.chromedriver().setup();

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");

        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);

//        //最大化
//        driver.manage().window().maximize();
//        Thread.sleep(3000);
//        //最小化
//        driver.manage().window().minimize();
//        Thread.sleep(3000);
//        //全屏
//        driver.manage().window().fullscreen();
//        Thread.sleep(3000);
//        //手动设置大小
//        driver.manage().window().setSize(new Dimension(1024,780));
//        Thread.sleep(3000);

        driver.get("https://image.baidu.com/");

//        //屏幕截图把页面加载后的场景拍照留存下来
//        File srcfile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//        //file -----> .png file
//        FileUtils.copyFile(srcfile,new File("test.png"));

        getScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName(),driver);

        driver.quit();
    }

    public void test04() throws InterruptedException {
        WebDriverManager.chromedriver().setup();

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");

        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);

        //访问百度首页
        driver.get("https://www.baidu.com/");

//        //点击百度图片，打开第二个标签页
//        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(6)")).click();
//
//        Thread.sleep(2000);

        //隐式等待
//        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));

        //显式等待
        WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(3));

        //等待输入框元素出现
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#chat-textarea")));

        //等待"百度一下"按钮可以点击
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#chat-submit-button")));

        wait.until(ExpectedConditions.urlToBe("https://www.biadu.com"));

//        driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("迪丽热巴");
//        driver.findElement(By.cssSelector("#chat-submit-button")).click();
//
//        driver.findElement(By.cssSelector("#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p"));



        //close
//        driver.close();
        driver.quit();
    }

    public void test05() throws InterruptedException {
        WebDriverManager.chromedriver().setup();

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");

        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);

        //请求url
        driver.navigate().to("https://tool.lu/");

        Thread.sleep(1000);
        //点击并在原页面跳转
        driver.findElement(By.cssSelector("#nav > div > div > ul > li:nth-child(6) > a")).click();
        Thread.sleep(1000);

        //后退--工具
        driver.navigate().back();
        Thread.sleep(1000);

        //前进--软件
        driver.navigate().forward();
        Thread.sleep(1000);

        //刷新--软件
        driver.navigate().refresh();
        Thread.sleep(1000);

        driver.quit();
    }


    public void test06() throws InterruptedException {
        WebDriverManager.chromedriver().setup();

        //谷歌浏览器
        ChromeOptions options = new ChromeOptions();

//        //edge浏览器配置
//        EdgeOptions options1 = new EdgeOptions();
//
//        //火狐
//        FirefoxOptions options2 = new FirefoxOptions();

        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");

        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);

//        //请求url---提示弹窗
//        driver.get("file:///C:/Users/w1917/Desktop/%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B/selenium-html/Prompt.html");
//        //点击调起弹窗
//        driver.findElement(By.cssSelector("body > input[type=button]")).click();
//
//        //切换弹窗
//        Alert alert = driver.switchTo().alert();
//
//        System.out.println("text:"+alert.getText());
//        Thread.sleep(2000);
//        alert.sendKeys("dismiss");
//        Thread.sleep(2000);
////        alert.dismiss();
//        //点击确认按钮
//        alert.accept();
//        Thread.sleep(2000);

        //警告弹窗
//        driver.get("file:///C:/Users/w1917/Desktop/测试用例/selenium-html/alert.html");
//        Thread.sleep(2000);
//        //调起弹窗
//        driver.findElement(By.cssSelector("#tooltip")).click();
//
//        //切换弹窗
//        Thread.sleep(2000);
//        Alert alert = driver.switchTo().alert();
////        //点击确认
////        Thread.sleep(2000);
////        alert.accept();
//
//        //点击取消
//        alert.dismiss();
//        Thread.sleep(2000);

        //文件上传
        driver.get("file:///C:/Users/w1917/Desktop/%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B/selenium-html/upload.html");
        Thread.sleep(3000);
        driver.findElement(By.cssSelector("body > div > div > input[type=file]")).sendKeys("C:\\Users\\w1917\\Desktop\\测试用例\\selenium-html\\test.txt");
        Thread.sleep(3000);
        driver.quit();
    }

    public void test07() throws IOException {
        WebDriverManager.chromedriver().setup();

        //谷歌浏览器
        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");

        //增加配置--无头模式
//        options.addArguments("-headless");

        //增加哎配置--设置浏览器加载策略
//        options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
//        options.setPageLoadStrategy(PageLoadStrategy.NONE);
        options.setPageLoadStrategy(PageLoadStrategy.EAGER);

        //1.打开浏览器
        WebDriver driver = new ChromeDriver(options);

        //请求百度网址
//        driver.get("http://www.baidu.com/");
//        //输入迪丽热巴
//        driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("迪丽热巴");
//        //点击百度一下按钮
//        driver.findElement(By.cssSelector("#chat-submit-button")).click();
//        getScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName(),driver);

        driver.get("https://www.bilibili.com/");

        //关闭浏览器
        driver.quit();
    }
}






















